#!/bin/bash
########################################################################
# script name:  sync_file2.sh
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2012/03/07    0.1     MDUMONT         initial
########################################################################
usage()
{
cat << STOP

usage: $0 [options] [ARG]

This script is used for ....

OPTIONS:
   -s               Source host and file/dir
   -d               Destination host and file/dir

Ex.:
$0 -s saphrt01:/etc/hosts -d saphrt02:/etc
$0 -s /etc/hosts -d saphrt02:/etc (from localhost to remote)
$0 -s saphrt02:/etc/hosts (from remote to local on the same destination as the source)
STOP
}
if [ `whoami` != root ]; then
   echo Please run this script as root or using sudo
   exit
fi
#######################################
# Get parameters
#######################################
while getopts s:d: OPTION
do
        case $OPTION in
        s)
                SOURCE=$(/bin/echo $OPTARG)
                ;;
        d)
                DEST=$(/bin/echo $OPTARG)
                ;;
        \?)
                usage
                exit 1
                ;;
        *)
                usage
                exit 1
                ;;
        esac
done
if [ $OPTIND = 1 ]; then usage; exit 1; fi
if [ -z "$SOURCE" ] && [ ! -z "$DEST" ]; then SOURCE=$DEST; fi
if [ ! -z "$SOURCE" ] && [ -z "$DEST" ]; then DEST=$SOURCE; fi
if [ -z "$SOURCE" ] && [ -z "$DEST" ]; then usage; exit 1; fi
EXCLUDELIST="/ /var /opt /usr"
#######################################
# Variables and environnement
#######################################
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else /bin/echo "No env library ($BAENV)"; exit 1; fi
SSH_OPTS="-o ConnectTimeout=15 -o BatchMode=yes -o StrictHostKeyChecking=no -o LogLevel=fatal -i /home/saunix/.ssh/id_rsa"
RSYNC="sudo /bin/rsync -avh --progress --log-file=/tmp/rsync.log"
WRAPPER=/var/code/sap/scripts/wrapper_sudo_rsync.sh
#######################################
# MAIN
#######################################
SHOST=${SOURCE%%:*}
SFILE=${SOURCE##*:}
DHOST=${DEST%%:*}
DFILE=${DEST##*:}
for i in $EXCLUDELIST
do
	if [ "$SFILE" = "$i" ]; then /bin/echo "Source file ($SFILE) is in EXCLUDELIST, exiting."; fi
	if [ "$DFILE" = "$i" ]; then /bin/echo "Destination file ($DFILE) is in EXCLUDELIST, exiting."; fi
done
if [ "$SHOST" == "$SFILE" ]; then SHOST=localhost; fi
if [ "$DHOST" == "$DFILE" ]; then DHOST=localhost; fi
/bin/echo "From $SHOST:$SFILE to $DHOST:$DFILE"
/bin/echo
read -n 1 -p "Is this what you realy want to do? [y/n]: "
/bin/echo
if [ "$REPLY" != "y" ]; then exit 1; fi
if [ "$SHOST" != "localhost" ] && [ "$DHOST" != "localhost" ]
then
	sudo -u saunix ssh $SSH_OPTS $SHOST $RSYNC --rsync-path="$WRAPPER -e \"ssh $SSH_OPTS\"" $SFILE saunix@$DHOST:$DFILE
elif [ "$SHOST" == "localhost" ] && [ "$DHOST" != "localhost" ]; then
	$RSYNC -e "ssh $SSH_OPTS" --rsync-path="$WRAPPER" $SFILE saunix@$DHOST:$DFILE
elif [ "$SHOST" != "localhost" ] && [ "$DHOST" == "localhost" ]; then
	$RSYNC -e "ssh $SSH_OPTS" --rsync-path="$WRAPPER" saunix@$SHOST:$SFILE $DFILE
elif [ "$SHOST" == "localhost" ] && [ "$DHOST" == "localhost" ]; then
	$RSYNC $SFILE $DFILE
fi
#######################################
if [ -f /tmp/rsync.log ]; then sudo chmod 755 /tmp/rsync.log; fi
exit 0
